/**
 * 云函数调用封装
 */

// 调用云函数的通用方法
const callFunction = (name, data = {}) => {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  
  return new Promise((resolve, reject) => {
    wx.cloud.callFunction({
      name,
      data,
      success: res => {
        if (res.result.code === 0) {
          resolve(res.result)
        } else {
          wx.showToast({
            title: res.result.message || '操作失败',
            icon: 'none'
          })
          reject(res.result)
        }
      },
      fail: err => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        })
        reject(err)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  })
}

// 获取装备列表
const getEquipmentList = (keyword = '', categoryId = '') => {
  return callFunction('getEquipmentList', { keyword, categoryId })
}

// 登录
const login = (userInfo) => {
  return callFunction('login', { userInfo })
}

// 借用装备
const borrowEquipment = (equipmentId) => {
  return callFunction('borrowEquipment', { equipmentId })
}

// 归还装备
const returnEquipment = (orderId, isDamaged = false) => {
  return callFunction('returnEquipment', { orderId, isDamaged })
}

// 获取订单列表
const getOrderList = (status = 'all') => {
  return callFunction('getOrderList', { status })
}

module.exports = {
  getEquipmentList,
  login,
  borrowEquipment,
  returnEquipment,
  getOrderList
} 